#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2022/6/13 16:00
   @File: 可疑交易规则评估_新增.py
"""
import random
import time

from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.variable import Variable

from BladeFerm.Lib.Common import data_processing, logger
from 线下客户管理.线下客户共用脚本.open_menu import open_menu
from 基础公共脚本.步骤检查_hui2 import FE, check_ele_text


@data_processing
def suspicious_trade_rule_add(input_data=None, ui_expect=None):
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': '可疑交易管理/可疑交易评估'})
    time.sleep(1)
    # 点击 新增
    FE.button("新增").click()

    # 填写表单
    ext = str(random.randint(0, 100))
    product_name = input_data.get('报表名称：', '')
    if product_name and product_name.endswith("**"):
        product_name = product_name.replace("**", ext)
        dic = {'报表名称：': product_name}
        input_data.update(dic)
    input_data.update({'起始交易时间：': f'2022/{random.randint(1, 7)}/{random.randint(1, 28)}'})
    input_data.update({'截止交易时间：': f'2022/{random.randint(8, 11)}/{random.randint(1, 30)}'})
    logger.info(f'fill data : {input_data}')
    idname = {'xpath': '//div[@class="h-modal-header"]/div[contains(text(),"新增可疑规则评估报表")]//'
                       'ancestor::div[@class="h-modal-content"]/div[@class="h-modal-body"]/form'}
    form = FE.form(idname=idname)
    form.auto_fill_group(input_data)
    form.click()

    # 点击提交
    express = f'//div[@class="h-modal-footer"]//button/span[contains(text(),"提交")]//parent::button'
    FE.button(idname={By.XPATH: express}).click()

    result = ui_expect.pop("期望结果", None)
    if result:
        check_ele_text(ui_expect={'期望结果': result})
    else:
        # 检查查询结果
        time.sleep(1)
        table = FE.table_new()
        table.check_expect_row(ui_expect, 1)

    # 设置返回变量
    variable = Variable()
    variable.set_variable('varc_report_name', product_name)



@data_processing
def suspicious_trade_rule_delete(input_data=None, ui_expect=None):
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': '可疑交易管理/可疑交易评估'})
    time.sleep(1)

    # 点击 新增
    table = FE.table_new()
    table.click_cell(input_data, {'操作': '删除'})

    # 确认是否删除
    option = ui_expect.get("是否继续")
    msg = FE.msg_box("删除该可疑规则评估报表")
    msg.click(option)

    # 判断结果
    if option == "确定":
        table.check_expect_row(input_data, 0)
    else:
        table.check_expect_row(input_data, 1)


